/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.posplus.losspreventiondataimporter.util;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 *
 * @author scottw
 */
public class Arguments {

    private boolean importCashiers;
    private boolean importProducts;
    private boolean importDepartments;
    private boolean importRegisters;
    private boolean importTransactions;
    private String configFile;
    private String logConfig;
    private Calendar start;
    private Calendar end;
    private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    public Arguments(String[] args) {
        importCashiers = false;
        importProducts = false;
        importDepartments = false;
        importRegisters = false;
        importTransactions = false;
        useParser(args);
    }

    public static Options constructOptions() {
        final Options options = new Options();
        options.addOption("c", false, "Import Cashiers");
        options.addOption("p", false, "Import Products");
        options.addOption("d", false, "Import Departments");
        options.addOption("r", false, "Import Registers");
        options.addOption("t", false, "Import Transactions");
        options.addOption("s", "start", true, "When importing transactions this is the start date in the format of yyyy-MM-dd");
        options.addOption("e", "end", true, "When importing transactions this is the end date in the format of yyyy-MM-dd");

        return options;
    }

    public static void printUsage(final String applicationName, final Options options, final OutputStream out) {
        final PrintWriter writer = new PrintWriter(out);
        final HelpFormatter usageFormatter = new HelpFormatter();
        usageFormatter.printUsage(writer, 80, applicationName, options);
        writer.close();
    }

    public static Calendar stringToCalendar(String s, SimpleDateFormat df) throws java.text.ParseException {
        Calendar cal = Calendar.getInstance();
        Date date = df.parse(s);
        cal.setTime(date);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
        return cal;
    }

    private void useParser(final String[] commandLineArguments) {
        final CommandLineParser cmdLineParser = new GnuParser();

        final Options options = constructOptions();
        CommandLine commandLine;
        try {
            commandLine = cmdLineParser.parse(options, commandLineArguments);
            if (commandLine.hasOption('h')) {
                printUsage("LossPreventionDataImporter", options, System.out);
            }
            if (commandLine.hasOption('c')) {
                importCashiers = true;
            }
            if (commandLine.hasOption('p')) {
                importProducts = true;
            }
            if (commandLine.hasOption('d')) {
                importDepartments = true;
            }
            if (commandLine.hasOption('r')) {
                importRegisters = true;
            }
            if (commandLine.hasOption('t') && commandLine.hasOption('s') && commandLine.hasOption('e')) {
                importTransactions = true;
                start = stringToCalendar(commandLine.getOptionValue("s"), df);
                end = stringToCalendar(commandLine.getOptionValue("e"), df);
                start.set(Calendar.HOUR_OF_DAY, 0);
                start.set(Calendar.MINUTE, 0);
                start.set(Calendar.SECOND, 0);
                end.set(Calendar.HOUR_OF_DAY, 23);
                end.set(Calendar.MINUTE, 59);
                end.set(Calendar.SECOND, 59);
            }
            if (commandLine.getArgs().length < 2) {
                configFile = null;
                logConfig = null;
            } else {
                configFile = commandLine.getArgs()[0];
                logConfig = commandLine.getArgs()[1];
            }

        } catch (ParseException e) {
            printUsage("LossPreventionDataImporter", options, System.out);
            System.exit(-1);
        } catch (java.text.ParseException ex) {
            printUsage("LossPreventionDataImporter", options, System.out);
            System.exit(-1);
        }
    }

    public Calendar getEnd() {
        return end;
    }

    public boolean isImportCashiers() {
        return importCashiers;
    }

    public boolean isImportDepartments() {
        return importDepartments;
    }

    public boolean isImportProducts() {
        return importProducts;
    }

    public boolean isImportRegisters() {
        return importRegisters;
    }

    public boolean isImportTransactions() {
        return importTransactions;
    }

    public Calendar getStart() {
        return start;
    }

    public String getConfigFile() {
        return configFile;
    }

    public void setConfigFile(String configFile) {
        this.configFile = configFile;
    }

    public String getLogConfig() {
        return logConfig;
    }

    public void setLogConfig(String logConfig) {
        this.logConfig = logConfig;
    }
}
